package tichu_project252.Round;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import tichu_project252.CardClasses.Card;
import tichu_project252.CardClasses.Phoenix;
import tichu_project252.Table;

public class ascendingPairs extends TurnType {

    /**
     * Returns true if the combination given is ascending pairs.
     *
     * <p> <b> Accessor(Selector): </b> isascendingPairs(LinkedList <Card> l)
     * </p>
     *
     * <p><b> Precondition: </b> The player should select some cards.</p> </p>
     * <p> <b> Postcondition:</b> Returns true if the combination is ascending
     * pairs, else it returns false. </p>
     *
     * @return if the combination is ascending pairs.
     */
    public static boolean isascendingPairs(LinkedList<Card> l, Table t) {
        LinkedList<Card> last = t.getLastDropped();

        //if the cards are not divided by two, then we're out
        if (l.size() % 2 == 0 && l.size() > 2) {
            //sort them
            Collections.sort(l, new CardComparator());
            System.out.println("hand:" + l + " table: " + last);

            //check if it has phoenix
            if (hasPhoenix(l)) {
                //if it does, set its value correctly
                setPhoenixValue(l);
            }

            //call sort again to place the phoenix card in its correct place
            Collections.sort(l, new CardComparator());
            System.out.println("hand:" + l + " table: " + last);

            //check if the cards have ascending values
            //if they don't, we're out
            if (!isascending(l)) {
                return false;
            }
            
            if (last.size() == 0) {
                return true;
            }else if (last.size() != l.size() ){
                return false;
            }
            //we've got ascending pairs!
            return true;
        } else {
            return false;
        }
    }

    private static void setPhoenixValue(LinkedList<Card> hand) {
        LinkedList<Integer> temp = new LinkedList<Integer>();
        int phoenixValue = 0;

        //find card that will be paired with phoenix first
        //it's gonna be like 2,2,3,3,5,6,6,P - so we need to find tha phoenix will
        //be paired with 5. In order to do this, we gradually change the Card
        //references until we find that their int representations all differ. 
        //e.g. 
        //for i = 1: (2, 2, 3) - only one differs, move on
        //for i = 2: (2, 3, 3) - only one differs, move on
        //for i = 3: (3, 3, 5) - only one differs, move on
        //for i = 4: (3, 5, 6) - found it! 5!
        for (int i = 1; i < hand.size() - 1; i++) {

            temp.add(hand.get(i - 1).getintRepr());
            temp.add(hand.get(i).getintRepr());
            temp.add(hand.get(i + 1).getintRepr());

            //remove duplicates
            temp = removeDuplicates(temp);

            //if all three are different
            if (temp.size() == 3) {
                //we've found the single card, since we got 3 different cards
                //need to store it to update the phoenix card later
                phoenixValue = hand.get(i).getintRepr();
                break;
            } else {
                temp.clear();
            }
        }
        if (phoenixValue != 0) {
            for (int i = 0; i < hand.size(); i++) {
                if ("Phoenix".equals(hand.get(i).getstringRepr())) {
                    hand.get(i).setintRepr(phoenixValue);
                }
            }
        }
    }

    //precon l.size() > 2h k;e
    private static boolean isascending(LinkedList<Card> hand) {
        LinkedList<Integer> temp = new LinkedList<Integer>();
        int max = hand.size() % 2;

        //go through the hand three cards at a time
        for (int i = 1; i < hand.size() - 1; i++) {
            //put their intReprs in a temp list
            temp.add(hand.get(i - 1).getintRepr());
            temp.add(hand.get(i).getintRepr());
            temp.add(hand.get(i + 1).getintRepr());

            //remove duplicates
            temp = TurnType.removeDuplicates(temp);
            Collections.sort(temp);
            System.out.println("-------------------------------------------");
            System.out.println(temp);

            //if all three are different then we're out
            if (temp.size() == 3) {
                System.out.println("3 wrong");
                return false;
                //if just two are different, then we need to check their values
            } else if (temp.size() == 2) {
                //if they are more than 1 point appart, e.g. 2,2,4,4, we're out
                if (temp.get(1) - temp.get(0) > 1) {
                    return false;
                }
            }
            //clear the list first and continue
            temp.clear();


        }
        return true;
    }
}
